<?php


namespace app\api\controller\v1;


use app\api\model\ACLModel;
use ApiOper;
use app\common\controller\UserBaseController;
use app\api\model\RoleModel;

class AclController extends UserBaseController
{
    function listAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'type' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $user_info = ApiOper::instance()->userinfo();

        $role_id = STAFF_ROLE_SUPER_ADMIN;
        if (isset($user_info['role_id'])){
            $role_id = $user_info['role_id'];
        }
        $level = (new RoleModel())->role_level($role_id);

        $list = (new ACLModel())->query_list($this->argv['type'], $level);
        $this->resp['list'] = $list;

        return $this->ret_resp();
    }

    function treeAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'type' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $list = (new ACLModel())->query_list($this->argv['type'], 3);

        $root = [
            'id' => 0,
            'children' => [],
        ];
        \common::list_to_tree($list, $root);

        $this->resp['tree'] = $root;

        return $this->ret_resp();
    }

    function setAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'type' => 'string',
            'version' => 'string',
            'list' => 'array',
        );

        $this->check_params();
        $this->check_token();

        (new ACLModel())->reset_uris($this->argv['version'], $this->argv['type'], $this->argv['list']);

        return $this->ret_resp();
    }

    function visiableAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'uri' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $this->resp['visitable'] = in_array($this->argv['uri'], $this->user_info['acl']);
        return $this->ret_resp();
    }

}